package com.ubtechinc.cruzr.libai.honglauncher;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.ubtechinc.cruzr.libai.honglauncher.db.AppInfoEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


public class LaunchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnLongClickListener {
    private LaunchAdapterItemClickListener listener;
    private List<ItemData> data;
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    private Map<RecyclerView.ViewHolder, Disposable> disposableMap = new HashMap<>();

    public LaunchAdapter(List<ItemData> data) {
        this.data = data;
    }

    public LaunchAdapter(LaunchAdapterItemClickListener listener, List<ItemData> data) {
        this.listener = listener;
        this.data = data;
    }

    public void setListener(LaunchAdapterItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.head_app, parent, false);
            return new FirstPageViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app, parent, false);
            return new CommonViewHolder(itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getType();
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        Disposable disposable = disposableMap.get(holder);
        if (disposable != null) {
            compositeDisposable.remove(disposable);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemData itemData = data.get(position);
        if (holder instanceof CommonViewHolder) {
            final AppInfoEntity appInfoEntity = itemData.getCommonAppInfo();
            ((CommonViewHolder) holder).setText(appInfoEntity.appname);
            disposableMap.put(holder,
                    RxView.clicks(holder.itemView)
                            .throttleFirst(500, TimeUnit.MILLISECONDS)
                            .subscribeOn(AndroidSchedulers.mainThread())
                            .subscribe(o -> {
                                if (listener != null) {
                                    listener.onCLickEvent(holder.itemView, appInfoEntity, position);
                                }
                            }));
            ((CommonViewHolder) holder).itemView.setOnLongClickListener(v -> {
                if (listener != null) {
                    listener.onLongCLickEvent(v, appInfoEntity, position);
                }
                return false;
            });
        } else if (holder instanceof FirstPageViewHolder) {

        } else {

        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public boolean onLongClick(View v) {
        return false;
    }


    private class FirstPageViewHolder extends RecyclerView.ViewHolder {
        private View itemView;

        public FirstPageViewHolder(View itemView) {
            super(itemView);
        }
    }

    private class CommonViewHolder extends RecyclerView.ViewHolder {
        private View itemView;

        CommonViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public CommonViewHolder setText(String text) {
            ((TextView) this.itemView.findViewById(R.id.app_name)).setText(text);
            return this;
        }

        public CommonViewHolder setVisible(int visible) {
            this.itemView.findViewById(R.id.app_delete).setVisibility(visible);
            return this;
        }

        public CommonViewHolder setOnClickListener(int id, View.OnClickListener listener) {
            this.itemView.findViewById(id).setOnClickListener(listener);
            return this;
        }

        int getIconId(Context context, String name) {
            if (TextUtils.isEmpty(name)) {
                return -1;
            }
            int id = context.getResources().getIdentifier(name, "mipmap", context.getPackageName());
            if (id == 0) {
                return -1;
            }
            return id;
        }
    }

}
